﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 折扣维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改折扣维护在一个页面。
/// </summary>
public partial class masterdata_Discounts_Type_List : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetDiscountsTypeBind();
        }
    }

    public string getStringType(object o)
    {
        int type = Int32.Parse(o.ToString());
        switch (type)
        {
            case 1:
                return "数量";//quantity
            case 2:
                return "金额";//monetary
        }
        return "";
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetDiscountsTypeBind()
    {

        gvChannelType.DataSource = DisCountManager.GetDiscount(MyStateManager.DomainID);
        gvChannelType.DataBind();
    }

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlPayType = DetailsView1.FindControl("ddlUoms") as DropDownList;
        DropDownList ddlType = DetailsView1.FindControl("ddlType") as DropDownList;
        CheckBox chk = DetailsView1.FindControl("chkYesNo") as CheckBox;
        CheckBox chk1 = DetailsView1.FindControl("chkcheck") as CheckBox;

        if (ddlPayType != null)
        {
            //绑定转换因子
            //ddlPayType.DataSource = UomsManager.GetUoms(MyStateManager.DomainID);
            //ddlPayType.DataTextField = "UomsDescription";
            //ddlPayType.DataValueField = "UomsCode";
            //ddlPayType.DataBind();
            //ddlPayType.Items.Insert(0, new ListItem("-选择规格-", ""));

            if (DetailsView1.DataItem != null)
            {
                ddlPayType.SelectedValue = ((DISCOUNTS)DetailsView1.DataItem).LEVELSUOM.ToString();
                ddlType.SelectedValue = ((DISCOUNTS)DetailsView1.DataItem).TYPE.ToString();
            }
            else
            {
                chk.Checked = true;
                chk1.Checked = true;
            }

        }
    }

    /// <summary>
    /// 保存数据方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DisCountManager manager = new DisCountManager();
            DISCOUNTS discount=null;
            TextBox ctl = DetailsView1.FindControl("txtSAPCode") as TextBox;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)  //新增
            {
                discount = new DISCOUNTS();

            }
            else //编辑
            {
                discount = manager.Load(int.Parse(gvChannelType.SelectedDataKey.Value.ToString()));
            }

            discount.SAPDISCCODE = ctl.Text;

            ctl = DetailsView1.FindControl("txtDespic") as TextBox;
            discount.DESCRIPTION = ctl.Text;
            ctl = DetailsView1.FindControl("txtLEV1") as TextBox;
            discount.LEVEL1 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtDiscount1") as TextBox;
            discount.DISCOUNT1 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtLEV2") as TextBox;
            discount.LEVEL2 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtDiscount2") as TextBox;
            discount.DISCOUNT2 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtLEV3") as TextBox;
            discount.LEVEL3 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtDiscount3") as TextBox;
            discount.DISCOUNT3 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtLEV4") as TextBox;
            discount.LEVEL4 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            ctl = DetailsView1.FindControl("txtDiscount4") as TextBox;
            discount.DISCOUNT4 = (string.IsNullOrEmpty(ctl.Text)) ? 0 : Decimal.Parse(ctl.Text);
            DropDownList ddlPayType = DetailsView1.FindControl("ddlType") as DropDownList;
            discount.TYPE = decimal.Parse(ddlPayType.SelectedValue);
            DropDownList ddlType = DetailsView1.FindControl("ddlUoms") as DropDownList;
            discount.LEVELSUOM = ddlType.SelectedValue;

            CheckBox ckbIsValid = DetailsView1.FindControl("chkYesNo") as CheckBox;
            discount.ACTIVE = ckbIsValid.Checked;
            CheckBox ckbCHECKLEVELS = DetailsView1.FindControl("chkcheck") as CheckBox;
            discount.CHECKLEVELS = ckbCHECKLEVELS.Checked ? "1" : "0";
            discount.LAST_UPDATE_TIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));

            discount.LAST_UPDATER = MyStateManager.Name;
            discount.DOMAINID = MyStateManager.DomainID;

            manager.Save(discount);
            GetDiscountsTypeBind();
            btnCancel_Click(sender, e);
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }

    }
    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        editArea.Visible = false;
        back.Attributes.Remove("class");
    }

    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvChannelType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
        SetStatus();
    }

    /// <summary>
    /// 设置状态
    /// </summary>
    private void SetStatus()
    {
        editArea.Visible = true;
        editArea.Style.Add("width", "60%");
        back.Attributes.Add("class", "back");
    }

    //新增
    protected void btnNew_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
        SetStatus();
    }

    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        GetDiscountsTypeBind();
    }
}
